package com.wang.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.dao.IAResultDao;
import com.wang.pojo.AResult;



/**
 * 管理员dao实现类
 * @author Zjx
 *
 */
public class AResultDaoImpl extends BaseDaoImpl<AResult> implements IAResultDao
{

	@Override
	public int saveOne(AResult t)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_result (emailId,rcpt,sender,url,fromname,anonStatus,whiteStatus,groupStatus,urlStatus,level,status,createTime,updateTime,pubTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
		
		paramsList.add(t.getEmailId());
		paramsList.add(t.getRcpt());
		paramsList.add(t.getSender());
		paramsList.add(t.getUrl());
		paramsList.add(t.getFromname());
		paramsList.add(t.getAnonStatus());
		paramsList.add(t.getWhiteStatus());
		paramsList.add(t.getGroupStatus());
		paramsList.add(t.getUrlStatus());
		paramsList.add(t.getLevel());
		paramsList.add(t.getStatus());
		paramsList.add(t.getCreateTime());
		paramsList.add(t.getUpdateTime());
		paramsList.add(t.getPubTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		t.setId(id);
		return res ; 
	}

	@Override
	public int updateOne(AResult t)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_result set ");
		sb.append("emailId=?,rcpt=?,sender=?,url = ? ,fromname=?, anonStatus=?,whiteStatus=?,urlStatus=?,groupStatus=?,level=?,status=?,createTime=?,updateTime=?,pubTime=?");
		sb.append(" where id = ? ");
		
		paramsList.add(t.getEmailId());
		paramsList.add(t.getRcpt());
		paramsList.add(t.getSender());
		paramsList.add(t.getUrl());
		paramsList.add(t.getFromname());
		paramsList.add(t.getAnonStatus());
		paramsList.add(t.getWhiteStatus());
		paramsList.add(t.getUrlStatus());
		paramsList.add(t.getGroupStatus());
		paramsList.add(t.getLevel());
		paramsList.add(t.getStatus());
		paramsList.add(t.getCreateTime());
		paramsList.add(t.getUpdateTime());
		paramsList.add(t.getPubTime());
		paramsList.add(t.getId());
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_result ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else if("truncate".equalsIgnoreCase(condMap.get("operType") + ""))
		{
			sb.delete(0, sb.length());
			sb.append("truncate table a_result") ; 
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public AResult findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_result ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else if(condMap.get("emailId") != null && !"".equalsIgnoreCase(condMap.get("emailId") + ""))
		{
			/* 邮件编号查询 */
			sb.append(" where emailId = ? ");
			paramsList.add(condMap.get("emailId"));
		}else
		{
			return null ; 
		}
		
		return this.findOneSql(sb.toString(), paramsList,AResult.class);
	}

	@Override
	public List<AResult> findCondList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_result where 1 = 1 ");
		
		/* 关键字 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sb.append(" and (rcpt like ? or sender like ? ) ");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* level：级别:0:普通;1:一般;2严重 */
		if(condMap.get("level") != null && !"".equalsIgnoreCase(condMap.get("level") + ""))
		{
			sb.append(" and level = ?  ");
			paramsList.add(condMap.get("level"));
		}
		
		/* 群发状态:0:否,1:是 */
		if(condMap.get("groupStatus") != null && !"".equalsIgnoreCase(condMap.get("groupStatus") + ""))
		{
			sb.append(" and groupStatus = ?  ");
			paramsList.add(condMap.get("groupStatus"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and lastLoginTime >= ? and lastLoginTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id desc ") ; 
		}else
		{
			sb.append(" order by pubTime desc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,AResult.class);
		}
		return this.findListSql(sb.toString(), paramsList,AResult.class); 
	}

	

}
